let socket: any;
function doConnect() {
	socket = new WebSocket("ws://localhost:13528");
	//如果是https的话，端口是13529
	//socket = new WebSocket('wss://localhost:13529');
	// 打开Socket
	socket.onopen = function (event: any) {
		console.log("连接成功");
		// 监听消息
		/**
		 * 请求打印机列表demo
		 * */
		let request = getRequestObject("getPrinters");
		socket.send(JSON.stringify(request));

		// socket.onmessage = function (event) {
		//   console.log(JSON.parse(event.data));
		//   let res = JSON.parse(event.data)
		//   if (res.cmd === 'getPrinters') {

		//   }

		// };
		// 监听Socket的关闭
		socket.onclose = function (event: any) {
			console.log("Client notified socket has closed", event);
		};
	};
	return socket;
}
/***
 *
 * 获取请求的UUID，指定长度和进制,如
 * getUUID(8, 2)   //"01001010" 8 character (base=2)
 * getUUID(8, 10) // "47473046" 8 character ID (base=10)
 * getUUID(8, 16) // "098F4D35"。 8 character ID (base=16)
 *
 */
function getUUID(len: number, radix: number) {
	let chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split("");
	let uuid: any[] = [],
		i;
	radix = radix || chars.length;
	if (len) {
		for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];
	} else {
		let r;
		uuid[8] = uuid[13] = uuid[18] = uuid[23] = "-";
		uuid[14] = "4";
		for (i = 0; i < 36; i++) {
			if (!uuid[i]) {
				r = 0 | (Math.random() * 16);
				uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];
			}
		}
	}
	return uuid.join("");
}
/***
 * 构造request对象
 */
function getRequestObject(cmd?: any) {
	let request = new Object() as any;
	request.requestID = getUUID(8, 16);
	request.version = "1.0";
	request.cmd = cmd;
	return request;
}
/***
 * 获取自定义区数据以及模板URL
 * waybillNO 电子面单号
 */
function getCustomAreaData(waybillNO: any) {
	//获取waybill对应的自定义区的JSON object，此处的ajaxGet函数是伪代码
	let jsonObject = waybillNO;
	let ret = new Object() as any;
	ret.templateURL = jsonObject.content.templateURL;
	ret.data = jsonObject.content.data;
	return ret;
}
/***
 * 获取电子面单Json 数据
 * waybillNO 电子面单号
 */
// function getWaybillJson(waybillNO) {
//   //获取waybill对应的json object，此处的ajaxGet函数是伪代码
//   var jsonObject = ajaxGet(waybillNO);
//   return jsonObject;
// }
/**
 * 打印电子面单
 * printer 指定要使用那台打印机
 * waybillArray 要打印的电子面单的数组
 */
function doPrint(arr: any, url: any, printer?: string, tip?: number, preview = false) {
	let request = getRequestObject("print");
	request.task = new Object();
	request.task.taskID = getUUID(8, 10);
	request.task.preview = preview;
	// request.task.notifyType = ['print'];
	request.task.printer = printer || "";
	request.task.previewType = "image";
	let documents: any = [];
	arr.forEach((i: any) => {
		let obj = new Object() as any;
		let objroda = new Object() as any;
		obj.documentID = i.tid + "|" + i.waybillCode;
		let templateURL = i.templateUrl ? i.templateUrl : url;
		obj.contents = [
			{
				data: i,
				templateURL: templateURL
			}
		];
		documents.push(obj);
	});
	request.task.documents = documents;
	socket.send(JSON.stringify(request));
}
//订单打印
function doPrintHandover(data: any, printer?: string) {
	let request = getRequestObject("print");
	request.task = new Object();
	request.task.taskID = getUUID(8, 10);
	request.task.preview = false;
	// request.task.notifyType = ['print'];
	request.task.printer = printer || "";
	request.task.previewType = "image";
	let documents: any = [];
	data.forEach((i: any) => {
		let obj = new Object() as any;
		obj.documentID = i.tid + "|" + i.waybillCode;
		let templateURL = i.templateURL;
		obj.contents = [
			{
				data: i,
				templateURL: templateURL
			}
		];
		documents.push(obj);
	});
	request.task.documents = documents;
	console.log(request);

	socket.send(JSON.stringify(request));
}
function doPrintPdf(arr: any, url: any, printer?: string) {
	let request = getRequestObject("print");
	request.task = new Object();
	request.task.taskID = getUUID(8, 10);
	request.task.preview = true;
	// request.task.notifyType = ['print'];
	request.task.printer = printer || "";
	request.task.previewType = "pdf";
	let documents: any = [];
	arr.forEach((i: any) => {
		let obj = new Object() as any;
		let objroda = new Object() as any;
		obj.documentID = i.documentID;
		obj.contents = [
			{
				data: i.content,
				templateURL: url
			}
		];
		documents.push(obj);
	});
	request.task.documents = documents;
	socket.send(JSON.stringify(request));
}
export { doConnect, doPrint, doPrintHandover, doPrintPdf };
